package com.android.uit;

import java.io.File;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AndroidFileManagerActivity extends ListActivity {
    /** Called when the activity is first created. */
    private enum DISPLAYMODE{ ABSOLUTE, RELATIVE; }
    
    private final DISPLAYMODE displayMode = DISPLAYMODE.ABSOLUTE;
    private List<IconfigText> directoryEntries = new ArrayList<IconfigText>();
    private File currentDirectory = new File("/");

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            // setContentView() gets called within the next line,
            // so we do not need it here.
            browseToRoot();
    }
   
    /**
     * This function browses to the
     * root-directory of the file-system.
     */
    private void browseToRoot() {
            browseTo(new File("/"));
}
   
    /**
     * This function browses up one level
     * according to the field: currentDirectory
     */
    private void upOneLevel(){
            if(this.currentDirectory.getParent() != null)
                    this.browseTo(this.currentDirectory.getParentFile());
    }
   
    private void browseTo(final File aDirectory){
            if (aDirectory.isDirectory()){
                    this.currentDirectory = aDirectory;
                    fill(aDirectory.listFiles());
            }
               
    }

    private void fill(File[] files) {
            this.directoryEntries.clear();

            // Add the "." == "current directory"          
            this.directoryEntries.add(new IconfigText(".", getResources().getDrawable(R.drawable.folder)));
            // and the ".." == 'Up one level'
            if(this.currentDirectory.getParent() != null)
                    this.directoryEntries.add(new IconfigText("..", getResources().getDrawable(R.drawable.uponelevel)));
           // xu ly viec load icon cho tung dong
            Drawable currentIcon=null;
            for(File currentFile:files)
            {
            	if (currentFile.isDirectory())
            	{
            		currentIcon= getResources().getDrawable(R.drawable.folder);
            	}
            	else 
            	{
            		String fileName=currentFile.getName();
            		if(checkEnds(fileName, getResources().getStringArray(R.array.fileEndingAudio)))
            			{
            				currentIcon=getResources().getDrawable(R.drawable.audio);
            			}
            		else if(checkEnds(fileName, getResources().getStringArray(R.array.fileEndingImage)))
        				{
        					currentIcon=getResources().getDrawable(R.drawable.image);
        				}
            		else if(checkEnds(fileName, getResources().getStringArray(R.array.fileEndingPackage)))
    				{
    					currentIcon=getResources().getDrawable(R.drawable.packed);
    				}
            		else if(checkEnds(fileName, getResources().getStringArray(R.array.fileEndingWebText)))
    				{
    					currentIcon=getResources().getDrawable(R.drawable.webtext);
    				}
            	}
            }
            switch(this.displayMode){
                    case ABSOLUTE:
                            for (File file : files){
                                this.directoryEntries.add(new IconfigText(file.getPath(), currentIcon));
                            }
                            break;
                    case RELATIVE: // On relative Mode, we have to add the current-path to the beginning
                            int currentPathStringLenght = this.currentDirectory.getAbsolutePath().length();
                            for (File file : files)
                            {
                                this.directoryEntries.add(new IconfigText(file.getAbsolutePath().substring(currentPathStringLenght), currentIcon));
                            }
                            break;
            }
           IconifiedTextListAdapter ita= new IconifiedTextListAdapter(this);
           ita.setListItems(directoryEntries);
           this.setListAdapter(ita);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
            int selectionRowID = position;
            String selectedFileString = this.directoryEntries.get(position).getText();
            if (selectedFileString.equals(".")) {
                    // Refresh
                    this.browseTo(this.currentDirectory);
            } else if(selectedFileString.equals("..")){
                    this.upOneLevel();
            } else {
                    File clickedFile = null;
                    switch(this.displayMode){
                            case RELATIVE:
                                    clickedFile = new File(this.currentDirectory.getAbsolutePath()
                                                                                            + this.directoryEntries.get(selectionRowID).getText());
                          
                                    break;
                            case ABSOLUTE:
                                    clickedFile = new File(this.directoryEntries.get(selectionRowID).getText());
                                    break;
                    }
                    if(clickedFile != null)
                            this.browseTo(clickedFile);
            }
    }
    /** Checks whether checkItsEnd ends with
     * one of the Strings from fileEndings */
    private boolean checkEnds(String checkItsEnd,
                        String[] fileEndings){
         for(String aEnd : fileEndings){
              if(checkItsEnd.endsWith(aEnd))
                   return true;
         }
         return false;
    }
}